/*
 * @lc app=leetcode.cn id=28 lang=javascript
 *
 * [28] 实现 strStr()
 */

// @lc code=start
/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function (haystack, needle) {
  if (needle.length === 0) return 0;
  if (haystack.length < needle.length) return -1;
  if (haystack.length === needle.length) return haystack === needle ? 0 : -1;

  let index = -1;
  for (let i = 0; i < haystack.length; i++) {
    if (haystack[i] !== needle[0]) {
      continue;
    }

    let j = 0,
      k = i;
    while (j < needle.length && k < haystack.length) {
      if (needle[j] !== haystack[k]) {
        break;
      }
      j++, k++;
    }

    if (j >= needle.length) {
      index = i;
      break;
    }
  }

  return index;
};
// @lc code=end

// @after-stub-for-debug-begin
module.exports = strStr;
// @after-stub-for-debug-end
